package com.checkbox.widgetset.client.ui;

import java.util.Collection;

import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.TableElement;
import com.google.gwt.dom.client.TableSectionElement;
import com.google.gwt.user.client.ui.ComplexPanel;
import com.google.gwt.user.client.ui.Widget;

public class VSelectableTree extends ComplexPanel {
  /** Set the CSS class name to allow styling. */
  public static final String CLASSNAME = "v-selectabletree";
  public static final String IS_STRUCTURE_CHANGED = "sc";
  private TableSectionElement tBody;
  private final TableElement table;
  private String collapseIcon;
  private String expandIcon;

  /**
   * The constructor should first call super() to initialize the component and then handle any
   * initialization relevant to Vaadin.
   */
  public VSelectableTree() {
    setElement(Document.get().createDivElement());
    table = Document.get().createTableElement();
    this.getElement().appendChild(table);
    table.setAttribute("width", "100%");
    setStyleName(CLASSNAME);
    this.tBody = Document.get().createTBodyElement();
    table.appendChild(tBody);
  }

  public void reconstruct(Collection<Widget> children) {
    // Remove all children
    while (getChildren().size() > 0) {
      remove(0);
    }
    // Remove DOM elements
    tBody.removeFromParent();
    // Reconstruct
    tBody = Document.get().createTBodyElement();
    table.appendChild(tBody);
    for (Widget child : children) {
      add(child, (com.google.gwt.user.client.Element) Element.as(tBody));
    }
  }

  public String getCollapseIcon() {
    return collapseIcon;
  }

  public void setCollapseIcon(String url) {
    this.collapseIcon = url;
  }

  public String getExpandIcon() {
    return expandIcon;
  }

  public void setExpandIcon(String url) {
    this.expandIcon = url;
  }
}
